from abc import ABC, abstractmethod


# Component 接口（抽象基类）
class Component(ABC):
    @abstractmethod
    def operation(self):
        pass


# ConcreteComponent 类，实现了 Component 接口
class ConcreteComponent(Component):
    def operation(self):
        return "ConcreteComponent Operation"


# Decorator 类，持有一个 Component 实例，并扩展其功能
class Decorator(Component):
    def __init__(self, component: Component):
        self._component = component

    def operation(self):
        # 默认情况下，调用被装饰组件的操作
        return self._component.operation()


# ConcreteDecoratorA 类，扩展了 Decorator 的功能
class ConcreteDecoratorA(Decorator):
    def __init__(self, component: Component):
        super().__init__(component)

    def operation(self):
        # 在被装饰组件的操作之前或之后添加额外的功能
        result = self._component.operation()
        return f"ConcreteDecoratorA({result})"


# ConcreteDecoratorB 类，扩展了 Decorator 的功能
class ConcreteDecoratorB(Decorator):
    def __init__(self, component: Component):
        super().__init__(component)

    def operation(self):
        # 在被装饰组件的操作之前或之后添加额外的功能
        result = self._component.operation()
        return f"ConcreteDecoratorB({result})"


# Client 类，使用 Component 接口
class Client:
    def __init__(self, component: Component):
        self._component = component

    def perform_operation(self):
        print(self._component.operation())


# 使用示例
if __name__ == "__main__":
    # 创建一个 ConcreteComponent 实例
    component = ConcreteComponent()

    # 使用 ConcreteDecoratorA 和 ConcreteDecoratorB 装饰该组件
    decorator_a = ConcreteDecoratorA(component)
    decorator_b = ConcreteDecoratorB(decorator_a)

    # 创建 Client 实例并执行操作
    client = Client(decorator_b)
    client.perform_operation()